/** ************************* M E D I A W I K I ******************** */
function isMediaWikiPageInEditMode() {
	if ((new GuiHandler).getMainWindow().content.document.getElementById("wpTextbox1") != null)
		return true;
	return false;
}

// ************* M e d i a W i k i C o n t e n t C o l l e c t o r C L A S S *************

function MediaWikiContentCollector() {

	var wiki_content = "";

	this.collectContentMediawiki = function(content_window) {
		// wiki_content_tmp = wiki_content;
		wiki_content = content_window.document.getElementById("wpTextbox1").value;
		return wiki_content;
	};

	this.cleanWikiContent = function(content) {
		wiki_content = content;
		if (wiki_content == null || wiki_content == "")
			return;

		removeWikiMarkup("{{", "}}");

		removeWikiTags("<ref", "</ref>");
		removeWikiTags("<gallery", "</gallery>");

		removeWikiTables();

		removeWikiMarkup("<", ">");

		removeWikiLinks("[[File:");
		removeWikiLinks("[[file:");
		removeWikiLinks("[[Media:");
		removeWikiLinks("[[media:");
		removeWikiLinks("[[Special:");
		removeWikiLinks("[[special:");
		removeWikiLinks("[[Category:");
		removeWikiLinks("[[category:");
		cleanWikiLinks();

		removeWikiMarkup("[http://", "]");
		removeWikiMarkup("http://", " ");
		removeWikiMarkup("[mailto:", "]");

		wiki_content = wiki_content.replaceAll("#REDIRECT", "");
		wiki_content = wiki_content.replaceAll("=", "");
		wiki_content = wiki_content.replaceAll("'", "");
		wiki_content = wiki_content.replaceAll("\"", "");
		wiki_content = wiki_content.replaceAll("----", "");
		wiki_content = wiki_content.replaceAll("*", "");
		wiki_content = wiki_content.replaceAll("#", "");
		wiki_content = wiki_content.replaceAll(";", "");
		wiki_content = wiki_content.replaceAll(":", " ");
		wiki_content = wiki_content.replaceAll("|", " ");
		wiki_content = wiki_content.replaceAll("{", "");
		wiki_content = wiki_content.replaceAll("}", "");
		wiki_content = wiki_content.replaceAll("[", "");
		wiki_content = wiki_content.replaceAll("]", "");
		wiki_content = wiki_content.replaceAll("\\", " ");
		wiki_content = wiki_content.replaceAll("/", "");
		wiki_content = wiki_content.replaceAll("(", "");
		wiki_content = wiki_content.replaceAll(")", "");
		wiki_content = wiki_content.replaceAll(",", " ");
		wiki_content = wiki_content.replaceAll("-", " ");
		wiki_content = wiki_content.replaceAll("+", " ");

		wiki_content = wiki_content.replace(/(\n|\t|\v|\f|\0|EOL)/g,
				" ");
		removeMulEmptySpace();
		return wiki_content;
	};

	function removeWikiMarkup(from, to) {
		var text = wiki_content;
		var start = text.indexOf(from);
		var finish = text.indexOf(to);

		while (start != -1 && finish != -1 && start < finish) {
			if (text.indexOf(from, start + from.length) != -1
					&& text.indexOf(from, start + from.length) < finish) {
				start = text.indexOf(from, start + from.length);
			} else {
				text = text.replace(text.substring(start, finish + to.length),
						"");
				start = text.indexOf(from);
				finish = text.indexOf(to);
			}
		}
		wiki_content = text;
	}

	function removeWikiTags(start_tag, end_tag) {
		var text = wiki_content;
		var reg = new RegExp(start_tag);

		var start_tag_index = text.indexOf(start_tag);
		var end_tag_index = text.indexOf(end_tag, start_tag_index);
		var end_start_tag_index = text.indexOf(">", start_tag_index
				+ start_tag.length);

		while (start_tag_index != -1
				&& (end_tag_index != -1 || end_start_tag_index != -1)) {
			if (text[end_start_tag_index - 1] == "/") {
				text = text.replace(text.substring(start_tag_index,
						end_start_tag_index + 1), "");
				start_tag_index = text.indexOf(start_tag);
				end_tag_index = text.indexOf(end_tag, start_tag_index);
				end_start_tag_index = text.indexOf(">", start_tag_index
						+ start_tag.length);
			} else if (text.substring(start_tag_index + start_tag.length,
					end_tag_index).search(reg) != -1) {
				start_tag_index = text.indexOf(start_tag, start_tag_index
						+ start_tag.length);
			} else {
				text = text.replace(text.substring(start_tag_index,
						end_tag_index + end_tag.length), "");
				start_tag_index = text.indexOf(start_tag);
				end_tag_index = text.indexOf(end_tag, start_tag_index);
				end_start_tag_index = text.indexOf(">", start_tag_index
						+ start_tag.length);
			}
		}
		wiki_content = text;
	}

	function removeWikiLinks(link_string) {
		var text = wiki_content;
		var fixed_start = text.indexOf(link_string);
		var start = text.indexOf(link_string);
		var finish = text.indexOf("]]", start);

		while (start != -1 && finish != -1 && start < finish) {
			if (text.substring(start + 2, finish).search(/\[\[/) != -1) {
				start = text.indexOf("[[", start + 2);
				finish = text.indexOf("]]", finish + 2);
			} else {
				text = text
						.replace(text.substring(fixed_start, finish + 2), "");
				fixed_start = text.indexOf(link_string);
				start = text.indexOf(link_string);
				finish = text.indexOf("]]", start);
			}
		}
		wiki_content = text;
	}

	// Cleans internal wiki links ie: [[Main Page]]
	function cleanWikiLinks() {
		var text = wiki_content;
		var fixed_start = text.indexOf("[[");
		var start = text.indexOf("[[");
		var finish = text.indexOf("]]");

		var just_one = true;

		while (start != -1 && finish != -1 && start < finish) {
			if (text.substring(start + 2, finish).search(/\[\[/) != -1) {
				just_one = false;
				start = text.indexOf("[[", start + 2);
				finish = text.indexOf("]]", finish + 2);
			} else if (text.substring(start, finish).search(/\|/) != -1
					&& just_one) {
				var subStr = text.substring(start, finish + 2);
				text = text.replace(subStr, subStr.substring(subStr
						.indexOf("|") + 1, subStr.indexOf("]]")));
				fixed_start = text.indexOf("[[");
				start = text.indexOf("[[");
				finish = text.indexOf("]]");
			} else if (text.substring(start, finish).search(/(#|:)/) != -1) {
				text = text
						.replace(text.substring(fixed_start, finish + 2), "");
				fixed_start = text.indexOf("[[");
				start = text.indexOf("[[");
				finish = text.indexOf("]]");
				just_one = true;
			} else if (just_one) {
				text = text.replace(text.substring(start, finish + 2), text
						.substring(start + 2, finish));
				fixed_start = text.indexOf("[[");
				start = text.indexOf("[[");
				finish = text.indexOf("]]");
			} else {
				text = text
						.replace(text.substring(fixed_start, finish + 2), "");
				fixed_start = text.indexOf("[[");
				start = text.indexOf("[[");
				finish = text.indexOf("]]");
				just_one = true;
			}
		}
		wiki_content = text;
	}

	function removeWikiTables() {
		var text = wiki_content;
		var fixed_start = text.indexOf("{|");
		var start = text.indexOf("{|");
		var finish = text.indexOf("|}");

		while (start != -1 && finish != -1 && start < finish) {
			if (text.substring(start + 2, finish).search(/\{\|/) != -1) {
				if (text[text.indexOf("\n", start) + 1] == "|"
						|| text[text.indexOf("\n", start) + 1] == "!")
					finish = text.indexOf("|}", finish + 2);
				else
					fixed_start = text.indexOf("{|", start + 2);

				start = text.indexOf("{|", start + 2);
			} else {
				var subString = text.substring(fixed_start, finish + 3);
				subString = removeLine(subString, "{|");
				subString = removeLine(subString, "|-");
				subString = removeLine(subString, "|+");
				subString = removeLine(subString, "|}");
				subString = removeStyleAttributes(subString);

				text = text.replace(text.substring(fixed_start, finish + 3),
						subString);
				fixed_start = text.indexOf("{|");
				start = text.indexOf("{|");
				finish = text.indexOf("|}");
			}
		}
		wiki_content = text;
	}

	// Removes the line that begins with quasi_tag
	function removeLine(subString, quasi_tag) {
		var start = subString.indexOf(quasi_tag);
		var finish = subString.indexOf("\n", start);

		while (start != -1 && finish != -1 && start < finish) {
			subString = subString.replace(subString.substring(start, finish),
					"");
			start = subString.indexOf(quasi_tag);
			finish = subString.indexOf("\n", start);
		}
		return subString;
	}

	// Removes all style for wiki tables
	function removeStyleAttributes(subString) {
		var splited_string_array = subString.split("\n");
		for ( var i = 0; i < splited_string_array.length; i++) {
			if (splited_string_array[i][0] == "!"
					&& splited_string_array[i].search(/\|/) != -1) {
				subString = subString.replace(
						splited_string_array[i].substring(0,
								splited_string_array[i].indexOf("|") + 1), "");
			}
			if (splited_string_array[i][0] == "|"
					&& splited_string_array[i].search(/\|\|/) != -1) {
				var cells = splited_string_array[i].split("||");
				for ( var j = 0; j < cells.length; j++) {
					if (cells[j].search(/\|/) != -1) {
						subString = subString.replace(cells[j].substring(0,
								cells[j].indexOf("|") + 1), "");
					}
				}
			}
		}
		subString = subString.replaceAll("!", "");
		return subString;
	}

	function removeMulEmptySpace() {
		var text = wiki_content;
		var start = text.indexOf("  ");

		while (start != -1) {
			text = text.replace(/\s\s/g, " ");
			start = text.indexOf("  ");
		}

		wiki_content = text;
	}

}

// Creates trim function for string objects
String.prototype.trim = function() {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
};

// Replaces all instances of the given substring.
// strTarget - the substring you want to replace
// strSubString - the string you want to replace in.
String.prototype.replaceAll = function(strTarget, strSubString) {
	var strText = this;
	var intIndexOfMatch = strText.indexOf(strTarget);

	while (intIndexOfMatch != -1) {
		strText = strText.replace(strTarget, strSubString);
		intIndexOfMatch = strText.indexOf(strTarget);
	}
	return (strText);
};

/** ********************************************************************** */

